package com.coffeeloversupportlibrary.coffeelover.mercury.utils.flow;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 盛守义
 * @createTime 2021/11/10 15:11
 * @projectName TurningCoffee
 * @className FlowLayout.java
 * @description TODO
 */
public class FlowLayout extends ViewGroup {

    public FlowLayout(Context context){
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for(int i = 0; i<count; i++){
            getChildAt(i).layout(l, t, r, b);
            l += getChildAt(i).getMeasuredWidth();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int totalWidth = MeasureSpec.getSize(widthMeasureSpec);
        int totalHeight = MeasureSpec.getSize(heightMeasureSpec);

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight() - getPaddingLeft();
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();

        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight  = MeasureSpec.getMode(heightMeasureSpec);

        final int count = getChildCount();
        for(int i = 0; i<count; i++){
            final View child = getChildAt(i);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(sizeWidth, modeWidth == MeasureSpec.EXACTLY ? MeasureSpec.AT_MOST : modeWidth);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(sizeHeight, modeHeight == MeasureSpec.EXACTLY ? MeasureSpec.AT_MOST : modeHeight);

            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }

        setMeasuredDimension(totalWidth, resolveSize(totalHeight, heightMeasureSpec));
    }
}

class Line{
    int mWidth = 0;
    int mHeight = 0;
    List<View> views = new ArrayList<>();

    public void addView(View view){
        views.add(view);
        mWidth += view .getMeasuredWidth();
        int childHeight = view.getMeasuredHeight();
        mHeight += view.getMeasuredHeight();
    }

    public void Layout(int l, int t){

    }
}
